package com.locationhistory.dao.impl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.locationhistory.bean.Device;
import com.locationhistory.dao.DeviceDAO;
import com.locationhistory.utils.Constants;

/**
 * 
 * @author thuanlv1
 *
 */
public class DeviceDAOImpl extends DatabaseHandler implements DeviceDAO {

	public DeviceDAOImpl(Context context) {
		super(context);
	}

	@Override
	public void saveDevice(Device device) {
		
		//open Database
		super.checkAndCopyDatabase();
		super.openDatabase();
		        
        ContentValues values = new ContentValues();
        values.put(Constants.Database.KEY_DEVICE_STATUS, device.getStatus());
        values.put(Constants.Database.KEY_DEVICE_UUID, device.getUuid());
        values.put(Constants.Database.KEY_DEVICE_TIMER, device.getTimer());
        values.put(Constants.Database.KEY_DEVICE_START_TIME, device.getStartTime());
        values.put(Constants.Database.KEY_DEVICE_STOP_TIME, device.getStopTime());
 
        // Inserting Row
        myDatabase.insert(Constants.Database.TABLE_DEVICE, null, values);
        myDatabase.close(); // Closing database connection
	} // end saveDevice(Device device)

	@Override
	public void updateDevice(Device device) {
		//open Database
		checkAndCopyDatabase();
        openDatabase();
		 
		ContentValues values = new ContentValues();
		values.put(Constants.Database.KEY_DEVICE_STATUS, device.getStatus());
        values.put(Constants.Database.KEY_DEVICE_UUID, device.getUuid());
        values.put(Constants.Database.KEY_DEVICE_TIMER, device.getTimer());
        values.put(Constants.Database.KEY_DEVICE_START_TIME, device.getStartTime());
        values.put(Constants.Database.KEY_DEVICE_STOP_TIME, device.getStopTime());
 
        // updating row
        myDatabase.update(Constants.Database.TABLE_DEVICE, values, Constants.Database.KEY_DEVICE_ID + " = ?",
                new String[] { String.valueOf(device.getId()) });
        
        myDatabase.close();
        
	} // updateDevice(Device device)

	@Override
	public Device getDeviceById(int deviceId) {
		checkAndCopyDatabase();
        openDatabase();
		 
        Cursor cursor = myDatabase.query(Constants.Database.TABLE_DEVICE, 
        		new String[] { Constants.Database.KEY_DEVICE_ID, 
        						Constants.Database.KEY_DEVICE_STATUS, 
        						Constants.Database.KEY_DEVICE_UUID, 
        						Constants.Database.KEY_DEVICE_TIMER,
        						Constants.Database.KEY_DEVICE_START_TIME,
        						Constants.Database.KEY_DEVICE_STOP_TIME
        						}, 
        						
        Constants.Database.KEY_DEVICE_ID + "=?", new String[] { String.valueOf(deviceId) }, null, null, null, null);
       
        Device device = new Device();
        if (cursor != null) {
        	cursor.moveToFirst();
        	device.setId(Integer.parseInt(cursor.getString(0)));
        	device.setStatus(Integer.parseInt(cursor.getString(1)));
        	device.setUuid(cursor.getString(2));
        	device.setTimer(cursor.getString(3));
        	device.setStartTime(cursor.getString(4));
        	device.setStopTime(cursor.getString(5));
        }
            
        myDatabase.close();
        // return device
        return device;
	} // end getDeviceById(int deviceId)

}
